<template>
    <el-container>
        <el-header style="margin-top: 20px;">
            <h2>建模代码展示</h2>
        </el-header>
        <el-main style="margin-top: -20px;">
            <el-card shadow="always" class="code-card">
                <el-scrollbar>
                    <pre class="code-block">
                        import pandas as pd
                        from sklearn.model_selection import train_test_split
                        from sklearn.preprocessing import StandardScaler, LabelEncoder
                        from sklearn.ensemble import RandomForestRegressor
                        from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
                        df = pd.read_excel('movies_data.xlsx')
                        X = df.drop(columns=['id', 'title', 'url', 'img_link', 'quote'])
                        
                        
                        scaler = StandardScaler()
                        X.loc[:, ['star', 'year']] = scaler.fit_transform(X[['star', 'year']])
                        
                        label_encoder = LabelEncoder()
                        X['regions'] = label_encoder.fit_transform(X['regions'])
                        X['director'] = label_encoder.fit_transform(X['director'])
                        X['actors'] = label_encoder.fit_transform(X['actors'])
                        
                        
                        X.columns = X.columns.astype(str)
                        
                        
                        y = df['number']
                        
                        
                        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
                        
                        
                        model = RandomForestRegressor(n_estimators=100, random_state=42)
                        model.fit(X_train, y_train)
                        
                        
                        y_pred = model.predict(X_test)
                        
                        
                        mae = mean_absolute_error(y_test, y_pred)
                        mse = mean_squared_error(y_test, y_pred)
                        r2 = r2_score(y_test, y_pred)
                        
                        print(f"Mean Absolute Error: {mae}")
                        print(f"Mean Squared Error: {mse}")
                        print(f"R-squared: {r2}")
            </pre>
                </el-scrollbar>
            </el-card>
        </el-main>
        <el-footer>
            <el-alert title="提示：请确保运行环境正确配置。" type="info" show-icon>
            </el-alert>
        </el-footer>
    </el-container>
</template>

<script>
    export default {
        methods: {
            runSpider() {
                console.log("爬虫任务开始...");
                // 在这里调用后端 API 启动爬虫任务
            },
            clearLogs() {
                console.log("日志已清除");
                // 实现清除日志功能
            },
        },
    };
</script>

<style scoped>
    /* .code-card { */
        /* margin: 20px 0; */
        /* padding: 10px; */
        /* background-color: #f5f5f5; */
        /* border-radius: 8px; */
    /* } */

    /* .code-block { */
        /* font-family: "Courier New", Courier, monospace; */
        /* font-size: 14px; */
        /* color: #333; */
        /* white-space: pre-wrap; */
        /* line-height: 1.5; */
    /* } */

    .button-group {
        display: flex;
        justify-content: space-between;
        margin-top: 0px;
    }
</style>